package scdm.team.noone.weatherlivewallpaper;



import java.util.Calendar;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;

public class SunView extends SurfaceView implements SurfaceHolder.Callback {
	private Bitmap mBK;
	private Bitmap mMoon;
	private Bitmap[] mGrass = new Bitmap[10];
	private Bitmap[] mSun = new Bitmap[2];
	private Paint mBackgroundPaint = new Paint();
	private Display mDisplay;
	private float h,w;
	private float A,B,C;
	private float x = 0;
	private int mPositionFrameGrass=0;
	private int mPositionFrameSun=0;
	private Bitmap[] mCloud = new Bitmap[8];
	private float mCloud_x = 0;
	
	
	public SunView(Context context) {
		super(context);
		init();
		mDisplay =((WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		h = mDisplay.getHeight();
		w = mDisplay.getWidth();
		A = (3*h)/(w*w);
		B = (-3*h)/w;
		C = h;
		mPositionFrameGrass=0;
		mPositionFrameSun=0;
	}
	
	public void init() {
		getHolder().addCallback(this);
		mBackgroundPaint.setColor(Color.BLUE);
		mBackgroundPaint.setFilterBitmap(true);
		mBK = BitmapFactory.decodeResource(getResources(), R.drawable.sky);
		mMoon =BitmapFactory.decodeResource(getResources(), R.drawable.layer_moon);
		mSun[0] = BitmapFactory.decodeResource(getResources(), R.drawable.sun1);
		mSun[1] = BitmapFactory.decodeResource(getResources(), R.drawable.sun2);
	
	
		initGrass();
		initCloud();
		
	}
	
	public void drawScreen(Canvas c) {
		c.drawBitmap(mBK, 0, 0, mBackgroundPaint);
		Calendar cal = Calendar.getInstance(); 
		float cal_Hour = cal.get(Calendar.HOUR);
		if (cal.get(Calendar.AM_PM) == Calendar.PM)
			cal_Hour += 12;
		float cal_Min = cal.get(Calendar.MINUTE);
		x = ((cal_Hour - 6) * 60 + cal_Min)*w/720;
		float y = A * x * x + B * x + C;
		
		c.drawBitmap(mSun[mPositionFrameSun%2], x- mSun[0].getWidth()/2, y, mBackgroundPaint);
		mPositionFrameSun++;
		if(mPositionFrameSun==2){
			mPositionFrameSun =0;
		}
		c.drawBitmap(mGrass[mPositionFrameGrass%10], -50, h-4*mGrass[0].getHeight()/5, mBackgroundPaint);
		mPositionFrameGrass ++;
		if(mPositionFrameGrass==10){
			mPositionFrameGrass=0;
		}
		Log.d("Tag", ":" + mCloud_x);
		if(mCloud_x > w)
			mCloud_x = -mCloud[0].getWidth();
		else
			mCloud_x += w/320;
		c.drawBitmap(mCloud[0], mCloud_x,20, mBackgroundPaint);
		c.drawBitmap(mCloud[1], mCloud_x - mCloud[1].getWidth()/2,70, mBackgroundPaint);
	}
	
	
	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		surfaceDestroyed(arg0);
		surfaceCreated(arg0);
		
	}
	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
	}
	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
	}
	
	private void initGrass(){
		mGrass[0] = BitmapFactory.decodeResource(getResources(), R.drawable.grassstand0);
		mGrass[1] = BitmapFactory.decodeResource(getResources(), R.drawable.grassstand1);
		mGrass[2] = BitmapFactory.decodeResource(getResources(), R.drawable.grassstand2);
		mGrass[3] = BitmapFactory.decodeResource(getResources(), R.drawable.grassstand3);
		mGrass[4] = BitmapFactory.decodeResource(getResources(), R.drawable.grassstand4);
		mGrass[5] = BitmapFactory.decodeResource(getResources(), R.drawable.grassstand5);
		mGrass[6] = BitmapFactory.decodeResource(getResources(), R.drawable.grassstand6);
		mGrass[7] = BitmapFactory.decodeResource(getResources(), R.drawable.grassstand7);
		mGrass[8] = BitmapFactory.decodeResource(getResources(), R.drawable.grassstand8);
		mGrass[9] = BitmapFactory.decodeResource(getResources(), R.drawable.grassstand9);
	}
	private void initCloud(){
		mCloud[0] = BitmapFactory.decodeResource(getResources(), R.drawable.cloud_01);
		mCloud[1] = BitmapFactory.decodeResource(getResources(), R.drawable.cloud_02);
		mCloud[2] = BitmapFactory.decodeResource(getResources(), R.drawable.cloud_03);
		mCloud[3] = BitmapFactory.decodeResource(getResources(), R.drawable.cloud_04);
		mCloud[4] = BitmapFactory.decodeResource(getResources(), R.drawable.cloud_05);
		mCloud[5] = BitmapFactory.decodeResource(getResources(), R.drawable.cloud_06);
		mCloud[6] = BitmapFactory.decodeResource(getResources(), R.drawable.cloud_07);
		mCloud[7] = BitmapFactory.decodeResource(getResources(), R.drawable.clouds_08);
	}
}
